package com.king.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.king.entity.MPost;
import com.king.search.model.PostDocument;
import com.king.search.mq.PostMQIndexMessage;
import com.king.search.repository.PostRepository;
import com.king.service.MPostService;
import com.king.service.SearchService;
import com.king.vo.MPostVo;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    PostRepository postRepository;

    @Autowired
    ModelMapper modelMapper;

    @Autowired
    MPostService mPostService;

    @Override
    public IPage search(Page page, String keyword) {
        if(keyword == null){//点分页时没传关键词过来
            return null;
        }

        //分页：mybatis plus的page 转成 jpa的page
        Long current = page.getCurrent() - 1;
        Long size = page.getSize();
        Pageable pageable = PageRequest.of(current.intValue(), size.intValue());

        //搜索es得到pageData
        MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyword, "title", "authorName", "categoryName");
        org.springframework.data.domain.Page<PostDocument> documents = postRepository.search(multiMatchQueryBuilder, pageable);

        //结果：jpa的pageData 转成 mybatisPlus的pageData
        IPage pageData = new Page(page.getCurrent(), page.getSize(), documents.getTotalElements());
        pageData.setRecords(documents.getContent());

        return pageData;
    }

    @Override
    public long initESData(List<MPostVo> records) {
        if (records == null || records.isEmpty()) {
            return 0;
        }

        List<PostDocument> documents = new ArrayList<>();
        for (MPostVo vo : records) {//映射转换
            PostDocument doc = modelMapper.map(vo, PostDocument.class);
            documents.add(doc);
        }

        postRepository.saveAll(documents);

        return documents.size();
    }

    @Override
    public void createOrUpdateIndex(PostMQIndexMessage message) {
        Long postId = message.getPostId();
        MPostVo postVo = mPostService.selectOnePost(new QueryWrapper<MPost>().eq("p.id", postId));
        if(postVo == null){
            return;
        }
        PostDocument document = modelMapper.map(postVo, PostDocument.class);
        postRepository.save(document);

        log.info("es 索引更新成功！ --> {}" + document.toString());
    }

    @Override
    public void removeIndex(PostMQIndexMessage message) {
        Long postId = message.getPostId();
        postRepository.deleteById(postId);

        log.info("es 索引删除成功！ --> {}" + message.toString());
    }
}
